package com.ruoyi.pom.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pom.domain.PomRecallTasks;
import com.ruoyi.pom.mapper.PomEbbinghausReviewMapper;
import com.ruoyi.pom.domain.PomEbbinghausReview;
import com.ruoyi.pom.service.IPomEbbinghausReviewService;

import javax.annotation.Resource;

/**
 * reviewService业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class PomEbbinghausReviewServiceImpl implements IPomEbbinghausReviewService 
{
    @Resource
    private PomEbbinghausReviewMapper pomEbbinghausReviewMapper;

    /**
     * 查询review
     * 
     * @param id review主键
     * @return review
     */
    @Override
    public PomEbbinghausReview selectPomEbbinghausReviewById(Long id)
    {
        return pomEbbinghausReviewMapper.selectPomEbbinghausReviewById(id);
    }

    /**
     * 查询review列表
     * 
     * @param pomEbbinghausReview review
     * @return review
     */
    @Override
    public List<PomEbbinghausReview> selectPomEbbinghausReviewList(PomEbbinghausReview pomEbbinghausReview)
    {
        return pomEbbinghausReviewMapper.selectPomEbbinghausReviewList(pomEbbinghausReview);
    }

    /**
     * 新增review
     * 
     * @param pomEbbinghausReview review
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPomEbbinghausReview(PomEbbinghausReview pomEbbinghausReview)
    {
        pomEbbinghausReview.setCreateTime(DateUtils.getNowDate());
        int rows = pomEbbinghausReviewMapper.insertPomEbbinghausReview(pomEbbinghausReview);
        insertPomRecallTasks(pomEbbinghausReview);
        return rows;
    }

    /**
     * 修改review
     * 
     * @param pomEbbinghausReview review
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePomEbbinghausReview(PomEbbinghausReview pomEbbinghausReview)
    {
        pomEbbinghausReview.setUpdateTime(DateUtils.getNowDate());
        pomEbbinghausReviewMapper.deletePomRecallTasksByEbbinghuasId(pomEbbinghausReview.getId());
        insertPomRecallTasks(pomEbbinghausReview);
        return pomEbbinghausReviewMapper.updatePomEbbinghausReview(pomEbbinghausReview);
    }

    /**
     * 批量删除review
     * 
     * @param ids 需要删除的review主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePomEbbinghausReviewByIds(Long[] ids)
    {
        pomEbbinghausReviewMapper.deletePomRecallTasksByEbbinghuasIds(ids);
        return pomEbbinghausReviewMapper.deletePomEbbinghausReviewByIds(ids);
    }

    /**
     * 删除review信息
     * 
     * @param id review主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePomEbbinghausReviewById(Long id)
    {
        //时间转化
        pomEbbinghausReviewMapper.deletePomRecallTasksByEbbinghuasId(id);
        return pomEbbinghausReviewMapper.deletePomEbbinghausReviewById(id);
    }

    /**
     * 新增recall信息
     * 
     * @param pomEbbinghausReview review对象
     */
    public void insertPomRecallTasks(PomEbbinghausReview pomEbbinghausReview)
    {
        List<PomRecallTasks> pomRecallTasksList = pomEbbinghausReview.getPomRecallTasksList();
        Long id = pomEbbinghausReview.getId();

        pomEbbinghausReview.setPomRecallTasksList(pomRecallTasksList);
        PomRecallTasks pomRecallTask=new PomRecallTasks();
        pomRecallTask.setEbbinghuasId(id);
        pomRecallTask.setContent(pomEbbinghausReview.getContent());
        pomRecallTask.setInitialStudyTime(pomEbbinghausReview.getInitialStudyTime());
        pomRecallTask.setTasksId(pomEbbinghausReview.getTasksId());
        pomRecallTasksList.add(pomRecallTask);
        if (StringUtils.isNotNull(pomRecallTasksList))
        {
            List<PomRecallTasks> list = new ArrayList<PomRecallTasks>();
            for (PomRecallTasks pomRecallTasks : pomRecallTasksList)
            {
                pomRecallTasks.setEbbinghuasId(id);
                list.add(pomRecallTasks);
            }
            if (list.size() > 0)
            {
                pomEbbinghausReviewMapper.batchPomRecallTasks(list);
            }
        }
    }
}
